<?php
global $_W, $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ('post' == $op) {
	$id = intval($_GPC['id']);
	if (!empty($id)) {
		$item = pdo_fetch("SELECT * FROM " . tablename($this->tablecaijiall) . " WHERE id = :id", array(':id' => $id));
		if (empty($item)) {
			message('数据不存在！', '', 'error');
		}
	}
	if (checksubmit('submit')) {
		$title = trim($_GPC['title']);
		if (empty($title)) {
			message('商品名称不能为空!');
		}
		$data = array('uniacid' => $_W['uniacid'], 'title' => $title, 'status' => $_GPC['status'], 'displayorder' => $_GPC['displayorder'],);
		if (empty($id)) {
			pdo_insert($this->modulename . '_cate', $data);
			message('商品添加成功！', $this->createWebUrl('listcaijiall', array('op' => 'display')), 'success');
		} else {
			pdo_update($this->modulename . '_cate', $data, array('id' => $id));
			message('商品更新成功！', $this->createWebUrl('listcaijiall', array('op' => 'display')), 'success');
		}
	}
} else if ('del' == $op) {
	$id = intval($_GPC['id']);
	$row = pdo_fetch("SELECT id FROM " . tablename($this->tablecaijiall) . " WHERE id = :id", array(':id' => $id));
	if (empty($row)) {
		message('商品不存在！');
	}
	pdo_delete($this->modulename . '_all_caiji', array('id' => $id));
	message('删除成功！', referer(), 'success');
} else if ('delall' == $op) {
	$ids = $_GPC['ids'];
	foreach ($ids as $id) {
		pdo_delete($this->modulename . '_all_caiji', array('id' => $id));
	}
	message('删除' . $id . '成功！', referer(), 'success');
} else if ('addall' == $op) {
	$ids = $_GPC['ids'];
	$cateid = $_GPC['cateid'];
	foreach ($ids as $id) {
		$caiji = pdo_fetch('select * from ' . tablename('bsht_tbk_all_caiji') . " where id = '{$id}' and uniacid = '{$_W['uniacid']}' order by itemfee desc,id asc limit 1");
		$item = pdo_fetch('select * from ' . tablename('bsht_tbk_all_item') . " where itemid = '{$caiji['itemid']}' and uniacid = '{$_W['uniacid']}' limit 1");
		if (empty($item)) {
			$data = array('uniacid' => $caiji['uniacid'], 'displayorder' => $caiji['displayorder'], 'itemid' => $caiji['itemid'], 'title' => $caiji['title'], 'd_title' => $caiji['d_title'], 'itempic' => $caiji['itempic'], 'shoptitle' => $caiji['shoptitle'], 'itemfee' => $caiji['itemfee'], 'itemfee2' => $caiji['itemfee2'], 'itemmsell' => $caiji['itemmsell'], 'itemstatus' => $caiji['itemstatus'], 'stime' => $caiji['stime'], 'etime' => $caiji['etime'], 'itemsurl' => $caiji['itemsurl'], 'itemurl' => $caiji['itemurl'], 'itemtkl' => $caiji['itemtkl'], 'itemyhj_zl' => $caiji['itemyhj_zl'], 'itemyhj_yl' => $caiji['itemyhj_yl'], 'itemyhj_tit' => $caiji['itemyhj_tit'], 'itemyhj_stime' => $caiji['itemyhj_stime'], 'itemyhj_etime' => $caiji['itemyhj_etime'], 'itemyhj_url' => $caiji['itemyhj_url'], 'itemyhj_tkl' => $caiji['itemyhj_tkl'], 'status' => $caiji['status'], 'cate_id' => $cateid, 'atime' => TIMESTAMP, 'fm' => $caiji['fm'], 'shouru_bili' => $caiji['shouru_bili'], 'shouru_yongjin' => $caiji['shouru_yongjin'], 'quan_id' => $caiji['quan_id'], 'maijia_id' => $caiji['maijia_id'], 'istmall' => $caiji['istmall'], 'quan_link' => $caiji['quan_link'],);
			pdo_insert($this->tableitemall, $data);
			pdo_delete($this->tablecaijiall, array('id' => $id));
		} else {
			$data = array('uniacid' => $caiji['uniacid'], 'displayorder' => $caiji['displayorder'], 'itemid' => $caiji['itemid'], 'title' => $caiji['title'], 'd_title' => $caiji['d_title'], 'itempic' => $caiji['itempic'], 'shoptitle' => $caiji['shoptitle'], 'itemfee' => $caiji['itemfee'], 'itemfee2' => $caiji['itemfee2'], 'itemmsell' => $caiji['itemmsell'], 'itemstatus' => $caiji['itemstatus'], 'stime' => $caiji['stime'], 'etime' => $caiji['etime'], 'itemsurl' => $caiji['itemsurl'], 'itemurl' => $caiji['itemurl'], 'itemtkl' => $caiji['itemtkl'], 'itemyhj_zl' => $caiji['itemyhj_zl'], 'itemyhj_yl' => $caiji['itemyhj_yl'], 'itemyhj_tit' => $caiji['itemyhj_tit'], 'itemyhj_stime' => $caiji['itemyhj_stime'], 'itemyhj_etime' => $caiji['itemyhj_etime'], 'itemyhj_url' => $caiji['itemyhj_url'], 'itemyhj_tkl' => $caiji['itemyhj_tkl'], 'status' => $caiji['status'], 'cate_id' => $cateid, 'atime' => TIMESTAMP, 'fm' => $caiji['fm'], 'shouru_bili' => $caiji['shouru_bili'], 'shouru_yongjin' => $caiji['shouru_yongjin'], 'quan_id' => $caiji['quan_id'], 'maijia_id' => $caiji['maijia_id'], 'istmall' => $caiji['istmall'], 'quan_link' => $caiji['quan_link'],);
			pdo_update($this->tableitemall, $data, array('id' => $item['id']));
			pdo_delete($this->tablecaijiall, array('id' => $id));
		}
	}
	message('导入成功，已自动更新重复商品的数据！', referer(), 'success');
} else if ('display' == $op) {
	if (checksubmit('submit')) {
		$displayorders = $_GPC['displayorder'];
		foreach ($displayorders as $id => $displayorder) {
			pdo_update($this->tablecaijiall, array('displayorder' => $displayorder), array('id' => $id));
		}
		message('批量更改排序成功', referer(), 'success');
	}
	$total2 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename($this->tablecaijiall) . " WHERE uniacid = '{$_W['uniacid']}'");
	$cate = pdo_fetchall("SELECT * FROM " . tablename($this->tablecateall) . " WHERE uniacid = '{$_W['uniacid']}' order by displayorder desc");
	$pindex = max(1, intval($_GPC['page']));
	if ($_GPC['psize']) {
		$psize = $_GPC['psize'];
	} else {
		$psize = 200;
	}
	$condition1 = "";
	if (!empty($_GPC['keywords'])) {
		$condition1 .= " AND CONCAT(`title`) LIKE '%{$_GPC['keywords']}%'";
	}
	$condition2 = "";
	if (!empty($_GPC['cate_id'])) {
		$condition2 .= " AND CONCAT(`cate_id`) = '{$_GPC['cate_id']}'";
	}
	$list = pdo_fetchall("SELECT * FROM " . tablename($this->tablecaijiall) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2 order by displayorder desc  LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
	$total = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename($this->tablecaijiall) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2 ");
	$pager = pagination($total, $pindex, $psize);
} else if ('status' == $op) {
	$id = $_GPC['id'];
	pdo_query('update ' . tablename($this->tablecaijiall) . " set status = !status where id='{$id}'");
	die('1');
}
function cloud_object_array($array)
{
	if (is_object($array)) {
		$array = (array)$array;
	}
	if (is_array($array)) {
		foreach ($array as $key => $value) {
			$array[$key] = cloud_object_array($value);
		}
	}
	return $array;
}

include $this->template('listcaijiall');